/*
 * xengine_mysql.c
 *
 *  Created on: 2009-8-10
 *      Author: wyb
 */

#include "xengine_mysql.h"

MYSQL mysql_conn; /* Connection handle */

void xengine_mysql_init(char *url, char *db, char *username, char *password) {
	if (mysql_init(&mysql_conn) == NULL) {
		wyb_log("xengine_mysql_init mysql_init error!");
	}
	if (mysql_real_connect(&mysql_conn, url, username, password, db,
			MYSQL_PORT, NULL, 0) == NULL) {
		wyb_log("xengine_mysql_init mysql_real_connect error!");
	}
}

void xengine_mysql_close() {
	mysql_close(&mysql_conn);
}

struct computer *get_computer(char *computer_name) {
	struct computer *result;

	char sql[500];
	sprintf(sql, "select id,name,ip from computer where name='%s'",
			computer_name);
	if (mysql_query(&mysql_conn, sql) == 0) {
		result = malloc(sizeof(struct computer));
		MYSQL_RES *mysql_result = mysql_store_result(&mysql_conn);
		int num_row = mysql_num_rows(mysql_result);

		if (num_row != 1)
			return NULL;
		MYSQL_ROW mysql_row = mysql_fetch_row(mysql_result);

		sscanf(mysql_row[0], "%d", &result->id);
		sscanf(mysql_row[1], "%s", result->computer_name);
		sscanf(mysql_row[2], "%s", result->ip);

		mysql_free_result(mysql_result);
	} else {
		wyb_log("get_computer mysql_query error!");
		return NULL;
	}
	return result;
}

struct app *get_app(char *app_name) {
	struct app *result;

	char sql[500];
	sprintf(sql, "select id,name,period from app where name='%s'", app_name);
	if (mysql_query(&mysql_conn, sql) == 0) {
		result = malloc(sizeof(struct app));
		MYSQL_RES *mysql_result = mysql_store_result(&mysql_conn);
		int num_row = mysql_num_rows(mysql_result);

		if (num_row != 1)
			return NULL;
		MYSQL_ROW mysql_row = mysql_fetch_row(mysql_result);
		sscanf(mysql_row[0], "%d", &result->id);
		sscanf(mysql_row[1], "%s", result->app_name);
		sscanf(mysql_row[2], "%lf", &result->period);

		mysql_free_result(mysql_result);
	} else {
		wyb_log("get_app mysql_query error!");
		return NULL;
	}
	return result;
}

struct app_array *get_apps() {
	struct app_array *result;

	char *sql = "select id,name,period from app";
	if (mysql_query(&mysql_conn, sql) == 0) {
		result = malloc(sizeof(struct app_array));
		MYSQL_RES *mysql_result = mysql_store_result(&mysql_conn);
		int num_row = mysql_num_rows(mysql_result);

		result->N = num_row;
		result->data = malloc(result->N * sizeof(struct app));

		int i;
		for (i = 0; i < num_row; i++) {
			MYSQL_ROW mysql_row = mysql_fetch_row(mysql_result);
			sscanf(mysql_row[0], "%d", &result->data[i].id);
			sscanf(mysql_row[1], "%s", result->data[i].app_name);
			sscanf(mysql_row[2], "%lf", &result->data[i].period);
		}
		mysql_free_result(mysql_result);
	} else {
		wyb_log("get_apps mysql_query error!");
		return NULL;
	}
	return result;
}

struct double_array *get_history(int app_id, char *field) {
	struct double_array *result;

	char sql[500];
	sprintf(sql,
			"select %s from app_history where app_id=%d order by time ASC",
			field, app_id);
	if (mysql_query(&mysql_conn, sql) == 0) {
		result = malloc(sizeof(struct double_array));
		MYSQL_RES *mysql_result = mysql_store_result(&mysql_conn);
		int num_row = mysql_num_rows(mysql_result);

		result->N = num_row;
		result->data = malloc(result->N * sizeof(struct app));

		int i;
		for (i = 0; i < num_row; i++) {
			MYSQL_ROW mysql_row = mysql_fetch_row(mysql_result);
			sscanf(mysql_row[0], "%lf", &result->data[i]);
		}
		mysql_free_result(mysql_result);
	} else {
		wyb_log("get_history mysql_query error!");
		return NULL;
	}
	return result;
}

struct double_array *get_part_history(int app_id, char *field, int num,
		time_t *new_time) {
	struct double_array *result;

	char sql[500];
	sprintf(
			sql,
			"select %s,time from app_history where app_id=%d order by time DESC limit 0,%d",
			field, app_id, num);
	if (mysql_query(&mysql_conn, sql) == 0) {
		result = malloc(sizeof(struct double_array));
		MYSQL_RES *mysql_result = mysql_store_result(&mysql_conn);
		int num_row = mysql_num_rows(mysql_result);

		result->N = num_row;
		result->data = malloc(result->N * sizeof(struct app));

		int i;
		for (i = 0; i < num_row; i++) {
			MYSQL_ROW mysql_row = mysql_fetch_row(mysql_result);
			sscanf(mysql_row[0], "%lf", &result->data[i]);
			if (i == num_row - 1) {
				sscanf(mysql_row[1], "%ld", new_time);
			}
		}
		mysql_free_result(mysql_result);
		for (i = 0; i < result->N / 2; i++) {
			double temp = result->data[i];
			result->data[i] = result->data[result->N - 1 - i];
			result->data[result->N - 1 - i] = temp;
		}
	} else {
		wyb_log("get_part_history mysql_query error!");
		return NULL;
	}
	return result;
}

int update_app_period(int app_id, double period) {
	char sql[500];
	sprintf(sql, "update app set period=%f where id=%d", period, app_id);
	if (mysql_query(&mysql_conn, sql) == 0) {

	} else {
		wyb_log("update_app_period mysql_query error!");
		return 0;
	}
	return 1;
}

int insert_forecast(int app_id, char *field, double value, time_t time) {
	char sql[500];
	sprintf(sql, "insert into app_forecast(app_id,%s,time) values (%d,%f,%ld)",
			field, app_id, value, time);
	if (mysql_query(&mysql_conn, sql) == 0) {

	} else {
		wyb_log("insert_forecast mysql_query error!");
		return 0;
	}
	return 1;
}

int insert_app_history(int app_id, char *field, double value, time_t time) {
	char sql[500];
	sprintf(sql, "insert into app_history(app_id,%s,time) values (%d,%f,%ld)",
			field, app_id, value, time);
	if (mysql_query(&mysql_conn, sql) == 0) {

	} else {
		wyb_log("insert_app_history mysql_query error!");
		return 0;
	}
	return 1;
}

int insert_app(char *app_name) {
	char sql[500];
	sprintf(sql, "insert into app(name,period) values ('%s',0.0)", app_name);
	if (mysql_query(&mysql_conn, sql) == 0) {

	} else {
		wyb_log("insert_app mysql_query error!");
		return 0;
	}
	return 1;
}

int insert_computer(char *computer_name) {
	char sql[500];
	sprintf(sql, "insert into computer(name,ip) values ('%s','')",
			computer_name);
	if (mysql_query(&mysql_conn, sql) == 0) {

	} else {
		wyb_log("insert_computer mysql_query error!");
		return 0;
	}
	return 1;
}

int insert_computer_app(int app_id, int computer_id, char *field, double value,
		time_t time) {
	char sql[500];
	sprintf(
			sql,
			"insert into computer_app(computer_id,app_id,%s,time) values (%d,%d,%f,%ld)",
			field, computer_id, app_id, value, time);
	if (mysql_query(&mysql_conn, sql) == 0) {

	} else {
		wyb_log("insert_computer_app mysql_query error!");
		return 0;
	}
	return 1;
}

int insert_computer_history(int computer_id, char *field, double value,
		time_t time) {
	char sql[500];
	sprintf(
			sql,
			"insert into computer_history(computer_id,%s,time) values (%d,%f,%ld)",
			field, computer_id, value, time);
	if (mysql_query(&mysql_conn, sql) == 0) {

	} else {
		wyb_log("insert_computer_history mysql_query error!");
		return 0;
	}
	return 1;
}
